package com.guoxin.concurrentprogramming.chapter2;

/**
 * Volatile保证可见性和有序性
 *
 * @author guoxin
 * @date 2019/7/15
 */
public class VolatileVisibilityAndOrderliness {
    private volatile static boolean ready;

    private volatile static int number;

    private static class ReadThread extends Thread {
        @Override
        public void run() {
            while(!ready){
                System.out.println(number);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new ReadThread().start();
        Thread.sleep(1000);
        number=42;
        ready=true;
        Thread.sleep(10000);
    }
}
